package oocl.com.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oocl.com.filter.impl.DataFilterImpl;
import oocl.com.pojo.Customer;
import oocl.com.service.CustomerManager;
import oocl.com.service.impl.CustomerManagerFactory;

/**
 * Servlet implementation class UpdateCustomerListServlet
 */
public class UpdateCustomerListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CustomerManager manager;
    public UpdateCustomerListServlet() {
        super();
        manager = CustomerManagerFactory.getInstance();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DataFilterImpl filter = new DataFilterImpl();
		//接参数
		String cid = request.getParameter("id");
		String cname = request.getParameter("name");
		String csex = request.getParameter("sex");
		String csal = request.getParameter("sal");
		String cbirth = request.getParameter("birth");
		String[] cfavs = request.getParameterValues("favs");
		//校验
		String temp = "";
		for(String i:cfavs){
			System.out.println(i);
			temp += i + ":";
		}
		temp = temp.substring(0,temp.length() - 1);
		System.out.println(temp);
		String data = cname + "," + csal + "," + csex + "," + cbirth + "," + temp;
		System.out.println(data);
		if(!filter.dataFilter(data)){
			System.out.println("error....");
			request.getRequestDispatcher("error.jsp").forward(request, response);
			return;
		}
		
		//类型转换
		Date birth = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			birth = sdf.parse(cbirth);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		boolean sex = Boolean.parseBoolean(csex);
		double sal = Double.parseDouble(csal);
		System.out.println(cid + "  cid ------------------");
		int id = Integer.parseInt(cid);
		//封装
		Customer c = new Customer(id, cname, sal, sex, birth, cfavs);
		//调业务
		int m = manager.updateCustomer(c);
		if(m>0){
			//存入context
			response.sendRedirect("list");//调用注册的servlet
		}
		else{
			request.getRequestDispatcher("error.jsp").forward(request, response);
//			response.sendRedirect("error.jsp");
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
